package kr.hkit.androidcomplete02.cp;

import kr.hkit.androidcomplete02.R;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

public class MyCursorAdapter extends CursorAdapter {
	private LayoutInflater mInflater;
	
	public MyCursorAdapter(Context context, Cursor c, boolean autoRequery) {
		super(context, c, autoRequery);
		mInflater = LayoutInflater.from(context);
	}
	
	class ViewHolder{
		TextView mTitle;
		TextView mSubTitle;
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		View itemLayout = mInflater.inflate(R.layout.product_list_row, null);
		ViewHolder viewHolder = new ViewHolder();
		viewHolder.mTitle = (TextView) itemLayout.findViewById(R.id.pdt_title);
		viewHolder.mSubTitle = (TextView) itemLayout.findViewById(R.id.pdt_subtitle);
		
		itemLayout.setTag(viewHolder);
		return itemLayout;
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		ViewHolder viewHolder = (ViewHolder) view.getTag();
		String name = cursor.getString(cursor.getColumnIndex("name"));
		String price = cursor.getString(cursor.getColumnIndex("price"));
		viewHolder.mTitle.setText(name);
		viewHolder.mSubTitle.setText(price);
	}

}
